package com.example.idachuappone.person.youhui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.example.idachuappone.BaseActivity;
import com.example.idachuappone.R;
import com.example.idachuappone.utils.CheckUtil;
import com.example.idachuappone.utils.ComUtil;
import com.example.idachuappone.utils.Constant;
import com.example.idachuappone.utils.MainToast;
import com.example.idachuappone.utils.NetUtil;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

public class AddYouHuiActivity extends BaseActivity implements OnClickListener {
	private Button btn_add;
	private Dialog dialog;
	private EditText et_quan_ma;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_you_hui);
		this.dialog = ComUtil.createCusProgressSmallBlackBgDialog(this);
		initView();
	}

	private void initView() {
		rightNone();
		this.tv_title.setText("\u6dfb\u52a0\u4f18\u60e0\u5238");
		this.btn_add.setOnClickListener(this);
		this.et_quan_ma = (EditText) findViewById(R.id.et_quan_ma);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back /* 2131165198 */:
			if (this.et_quan_ma.getText().toString() == null
					|| this.et_quan_ma.getText().toString().replace(" ", "").trim().length() <= 0) {
				MainToast.show(this, "\u8bf7\u8f93\u5165\u4f18\u60e0\u7801\uff01", 0);
			} else if (CheckUtil.checkNet(this)) {
				Map<String, String> map = new HashMap();
				map.put("code", this.et_quan_ma.getText().toString().replace(" ", "").trim());
				this.dialog.show();
				NetUtil.post(this, Constant.YOUHUIGETQUAN, map, new RequestCallBack<String>() {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						AddYouHuiActivity.this.dialog.dismiss();
						try {
							JSONObject jsonObject = new JSONObject(NetUtil.unescapeUnicode(responseInfo.result));
							if (jsonObject.getInt("code") == 10000) {
								MainToast.show(AddYouHuiActivity.this, "\u6dfb\u52a0\u6210\u529f\uff01", 0);
								AddYouHuiActivity.this.setResult(-1);
								AddYouHuiActivity.this.finish();
								return;
							}
							MainToast.show(AddYouHuiActivity.this, jsonObject.getString("error"), 0);
						} catch (Exception e) {
							e.printStackTrace();
							MainToast.show(AddYouHuiActivity.this, "\u670d\u52a1\u5668\u5fd9", 0);
						}
					}

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						AddYouHuiActivity.this.dialog.dismiss();
						MainToast.show(AddYouHuiActivity.this,
								AddYouHuiActivity.this.getResources().getString(R.string.netFail), 0);
					}
				});
			} else {
				MainToast.show(this, getResources().getString(R.string.txt_network), 0);
			}
		default:
		}
	}
}
